package com.xiaoxu.rental.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xiaoxu.rental.entity.Dept;
import com.xiaoxu.rental.mapper.DeptMapper;
import com.xiaoxu.rental.service.DeptService;
import com.xiaoxu.rental.util.DeptTreeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author xtq
* @description 针对表【sys_dept】的数据库操作Service实现
* @createDate 2024-07-26 08:40:20
*/
@Service
@Transactional
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Override
    public List<Dept> selectList(Dept dept) {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(dept.getDeptName()),
                "dept_name", dept.getDeptName());
        queryWrapper.orderByAsc("order_num");
        List<Dept> depts = baseMapper.selectList(queryWrapper);
            //查询每个部门的子部门
        return DeptTreeUtils.buildTree(depts,0);

    }

    @Override
    public List<Dept> selectTree() {
        QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("order_num");
        List<Dept> depts = baseMapper.selectList(queryWrapper);
        Dept dept=new Dept();
        dept.setDeptName("所有部门").setId(0).setPid(-1);
        depts.add(dept);
        return DeptTreeUtils.buildTree(depts,-1);
    }

    @Override
    public Boolean hasChildren(Integer id) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id);
        return baseMapper.selectCount(wrapper) > 0;
    }

}




